import { Tool, ToolColor } from '../../../Tool';

/**
 * 高程测量
 */
export class ElevationTool extends Tool {
	constructor(viewer, options = {}, ...args) {
		super(arguments);
		this.viewer = viewer;
		this.options = options;
		this.float = options?.float ?? 2;

		this.clickHandler = new Cesium.ScreenSpaceEventHandler(this.viewer.scene.canvas);
		this.handler = new Cesium.MeasureHandler(viewer, Cesium.MeasureMode.DVH);
		this.handler.lineColor = Cesium.Color.fromCssColorString(ToolColor.functionalColor);
		viewer.scene.layers.layerQueue.forEach((item) => {
			item.selectEnabled = false;
			item.releaseSelection();
		});
		[...arguments].forEach((item) => {
			if (typeof item === 'function') {
				this.callback = item;
			}
		});
		this.callbackValue = {
			total: null,
			current: null,
			tool: this
		};
	}

	start() {
		let count = 0;
		let vertical, spatial, horizontal;

		this.clickHandler.setInputAction((e) => {
			++count;
			if (count === 2) {
				this.callbackValue.current = { vertical, spatial, horizontal };
				this.callback && this.callback(this.callbackValue);
			}
		}, Cesium.ScreenSpaceEventType.LEFT_CLICK);

		this.handler.measureEvt.addEventListener((result) => {
			const { distance, verticalHeight, horizontalDistance } = result;

			spatial =
				distance > 1000
					? (distance / 1000).toFixed(this.float) + 'km'
					: Number(distance).toFixed(this.float) + 'm';
			vertical =
				verticalHeight > 1000
					? (verticalHeight / 1000).toFixed(this.float) + 'km'
					: Number(verticalHeight).toFixed(this.float) + 'm';
			horizontal =
				horizontalDistance > 1000
					? (horizontalDistance / 1000).toFixed(this.float) + 'km'
					: Number(horizontalDistance).toFixed(this.float) + 'm';

			const { disLabel, vLabel, hLabel } = this.handler;
			[disLabel, vLabel, hLabel].forEach((item) => {
				item.font = '14px sans-serif';
				item.showBackground = true;
				item.backgroundColor = Cesium.Color.fromCssColorString(
					ToolColor.toRgba(ToolColor.backgroundColor, 0.8)
				);
				item.fillColor = Cesium.Color.fromCssColorString(ToolColor.color);
				item.style = Cesium.LabelStyle.FILL;
				item.verticalOrigin = Cesium.VerticalOrigin.TOP;
				item.pixelOffset = new Cesium.Cartesian2(0, 0);
			});

			disLabel.text = '空间距离:' + spatial;
			disLabel.distanceDisplayCondition = new Cesium.DistanceDisplayCondition(
				0,
				result.horizontalDistance * 5
			);

			vLabel.text = '垂直高度:' + vertical;

			hLabel.text = '水平距离:' + horizontal;
			hLabel.distanceDisplayCondition = new Cesium.DistanceDisplayCondition(
				0,
				result.verticalHeight * 20
			);
			hLabel.pixelOffset = new Cesium.Cartesian2(12, 0);
			hLabel.verticalOrigin = Cesium.VerticalOrigin.BOTTOM;
		});

		this.handler.activeEvt.addEventListener((isActive) => {
			if (isActive === true) {
				this.viewer.enableCursorStyle = true;
				this.viewer._element.style.cursor = 'Crosshair';
			} else {
				this.viewer.enableCursorStyle = false;
				this.viewer._element.style.cursor = '';
			}
		});

		this.handler.activate();
	}

	cancel() {
		this.clickHandler?.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK);
		this.handler.clear();
		this.handler.deactivate();
	}
}
ElevationTool.toolId = 'ElevationTool';
